package com.kcl.dfss.utils;


import android.content.ContentValues;
import android.content.Context;
import android.database.Cursor;
import android.database.sqlite.SQLiteDatabase;
import android.database.sqlite.SQLiteDatabase.CursorFactory;
import android.database.sqlite.SQLiteOpenHelper;

import com.kcl.dfss.utils.UsageRecordContract.RecordEntry;

public class DatabaseUtils extends SQLiteOpenHelper {
	public static final String TEXT_TYPE = " TEXT";
	public static final String FLOAT_TYPE = " FLOAT";
	public static final String COMMA_SEP = ",";
	public SQLiteDatabase mDatabase;
	public String SQL_CREATE_TABLE = "CREATE TABLE "
			+ UsageRecordContract.RecordEntry.TABLE_CONTENT + " ("
			+ RecordEntry._ID + " INTEGER PRIMARY KEY,"
			+ RecordEntry.COLUMN_NAME_ENTRY_ID + TEXT_TYPE + COMMA_SEP
			+ RecordEntry.COLUMN_NAME_ENTRY_DATE + TEXT_TYPE + COMMA_SEP
			+ RecordEntry.COLUMN_NAME_ENTRY_START_TIME + TEXT_TYPE + COMMA_SEP
			+ RecordEntry.COLUMN_NAME_ENTRY_END_TIME + TEXT_TYPE + COMMA_SEP
			+ RecordEntry.COLUMN_NAME_ENTRY_DISTANCE + TEXT_TYPE + COMMA_SEP
			+ RecordEntry.COLUMN_NAME_ENTRY_AVESPEED + FLOAT_TYPE + " )";
	public String SQL_DELETE_TABLE = "DROP TABLE IF EXISTS "+RecordEntry.TABLE_CONTENT;
	
	public DatabaseUtils(Context context){
		super(context,RecordEntry.TABLE_CONTENT,null,1);
	}
	public DatabaseUtils(Context context, String name, CursorFactory factory,
			int version) {
		super(context, name, factory, version);
		// TODO Auto-generated constructor stub
	}
	
	public DatabaseUtils(DatabaseItem item, Context context, String name, CursorFactory factory,
			int version) {
		super(context, name, factory, version);
		// TODO Auto-generated constructor stub
		setDatabaseItem();
	}
	private void setDatabaseItem(){
		
	}

	public void createDatabase() {

	}
	public void insert(ContentValues value){
		SQLiteDatabase db = getWritableDatabase();
		db.insert(RecordEntry.TABLE_CONTENT,null , value);
	}
	public Cursor query(String[] projection,String selection,String[] selectionArgs,String order){
		SQLiteDatabase db = getWritableDatabase();
		Cursor c = db.query(RecordEntry.TABLE_CONTENT,
				projection,
				selection,
				selectionArgs,
				null,
				null,
				order);
		return c;
	}
	public void delete(String selection,String[] selectionArgs){
		SQLiteDatabase db = getWritableDatabase();
		db.delete(RecordEntry.TABLE_CONTENT,selection,selectionArgs);
	}
	@Override
	public void onCreate(SQLiteDatabase db) {
		// TODO Auto-generated method stub
		db.execSQL(SQL_CREATE_TABLE);
	}

	@Override
	public void onUpgrade(SQLiteDatabase db, int oldVersion, int newVersion) {
		// TODO Auto-generated method stub
		db.execSQL(SQL_DELETE_TABLE);
		onCreate(db);
	}
}
